/*
 * can.h
 *
 * Created: 01.12.2013 20:11:56
 *  Author: User
 */ 


#ifndef CAN_H_
#define CAN_H_
#include <stdint.h>
#include <avr/io.h>
#include <stdio.h>
#include <string.h>
#include <can_msg.h>
#define select(r) PORTC.OUTCLR=PIN4_bm; // CS=0
#define unselect(r) PORTC.OUTSET=PIN4_bm; // CS=1


//MCP2515 SPI instructions
#define	SPI_READ_RX		0x90
#define MCP2515_RESET                0xC0
#define MCP2515_READ                0x03
#define MCP2515_READ_BUF_RXB0SIDH    0x90    //read data from begin address of Standard ID (MSB) of RX buffer 0
#define MCP2515_READ_BUF_RXB0D0        0x92    //read data from begin address of data byte 0 of RX buffer 0
#define MCP2515_READ_BUF_RXB1SIDH    0x94    //...RX buffer 1
#define MCP2515_READ_BUF_RXB1D0        0x96    //...RX buffer 1
#define MCP2515_WRITE                0x02
#define MCP2515_LOAD_BUF_TXB0SIDH    0x40    //load data from begin address of Standard ID (MSB) of TX buffer 0
#define MCP2515_LOAD_BUF_TXB0D0        0X41    //load data from begin address of data byte 0 of TX buffer 0
#define MCP2515_LOAD_BUF_TXB1SIDH    0x42    //...TX buffer 1
#define MCP2515_LOAD_BUF_TXB1D0        0X43    //...TX buffer 1
#define MCP2515_LOAD_BUF_TXB2SIDH    0x44    //...TX buffer 2
#define MCP2515_LOAD_BUF_TXB2D0        0X45    //...TX buffer 2
#define MCP2515_RTS_TXB0            0x81    //activate the RTS of TX buffer 0.
#define MCP2515_RTS_TXB1            0x82    //...TX buffer 1.
#define MCP2515_RTS_TXB2            0x84    //...TX buffer 2.
#define MCP2515_RTS_ALL                0x87    //...TX buffer 0, 1 and 2.
#define MCP2515_READ_RXTX_STATUS    0xA0
#define MCP2515_RX_STATUS            0xB0
#define MCP2515_BIT_MODIFY            0x05


#define MCP2515_CANSTAT        0x0E    //CAN status. any addr of MSB will give the same info???

#define MCP_RXF0SIDH	0x00
#define MCP_RXF0SIDL	0x01
#define MCP_RXF0EID8	0x02
#define MCP_RXF0EID0	0x03
#define MCP_RXF1SIDH	0x04
#define MCP_RXF1SIDL	0x05
#define MCP_RXF1EID8	0x06
#define MCP_RXF1EID0	0x07
#define MCP_RXF2SIDH	0x08
#define MCP_RXF2SIDL	0x09
#define MCP_RXF2EID8	0x0A
#define MCP_RXF2EID0	0x0B
#define MCP_CANSTAT		0x0E
#define MCP_CANCTRL		0x0F
#define MCP_RXF3SIDH	0x10
#define MCP_RXF3SIDL	0x11
#define MCP_RXF3EID8	0x12
#define MCP_RXF3EID0	0x13
#define MCP_RXF4SIDH	0x14
#define MCP_RXF4SIDL	0x15
#define MCP_RXF4EID8	0x16
#define MCP_RXF4EID0	0x17
#define MCP_RXF5SIDH	0x18
#define MCP_RXF5SIDL	0x19
#define MCP_RXF5EID8	0x1A
#define MCP_RXF5EID0	0x1B
#define MCP_TEC			0x1C
#define MCP_REC			0x1D
#define MCP_RXM0SIDH	0x20
#define MCP_RXM0SIDL	0x21
#define MCP_RXM0EID8	0x22
#define MCP_RXM0EID0	0x23
#define MCP_RXM1SIDH	0x24
#define MCP_RXM1SIDL	0x25
#define MCP_RXM1EID8	0x26
#define MCP_RXM1EID0	0x27
#define MCP_CNF3		0x28
#define MCP_CNF2		0x29
#define MCP_CNF1		0x2A
#define MCP_CANINTE		0x2B
#define MCP_CANINTF		0x2C
#define MCP_EFLG		0x2D
#define MCP_TXB0CTRL	0x30
#define MCP_TXB1CTRL	0x40
#define MCP_TXB2CTRL	0x50
#define MCP_RXB0CTRL	0x60
#define MCP_RXB0SIDH	0x61
#define MCP_RXB0SIDL	0x62
#define MCP_RXB1CTRL	0x70
#define MCP_RXB1SIDH	0x71


//MCP2515 control settings register address.
//All can be used with the Bit Modify command, except the CANSTAT register.
#define MCP2515_BFPCTRL        0x0C    //RXnBF pin control/status
#define MCP2515_TXRTSCTRL    0x0D    //TXnRTS pin control/status
#define MCP2515_CANSTAT        0x0E    //CAN status. any addr of MSB will give the same info???
#define MCP2515_CANCTRL        0x0F    //CAN control status. any addr of MSB will give the same info???
#define MCP2515_TEC            0x1C    //Transmit Error Counter
#define MCP2515_REC            0x1D    //Receive Error Counter
#define MCP2515_CNF3        0x28    //Phase segment 2
#define MCP2515_CNF2        0x29    //Propagation segment & Phase segment 1 & n sample setting
#define MCP2515_CNF1        0x2A    //Baud rate prescaler & Sync Jump Width
#define MCP2515_CANINTE        0x2B    //CAN interrupt enable
#define MCP2515_CANINTF        0x2C    //Interrupt flag
#define MCP2515_EFLG        0x2D    //Error flag
#define MCP2515_TXB0CTRL    0x30    //TX buffer 0 control
#define MCP2515_TXB1CTRL    0x40    //TX buffer 1 control
#define MCP2515_TXB2CTRL    0x50    //TX buffer 2 control
#define MCP2515_RXB0CTRL    0x60    //RX buffer 0 control
#define MCP2515_RXB1CTRL    0x70    //RX buffer 1 control



#define RXM1		6

#define RXM0		5

#define RXRTR		3

#define BUKT		2

#define BUKT1		1

#define FILHIT0		0


#define TXB0CTRL	0x30

#define TXB0SIDH	0x31

#define TXB0SIDL	0x32

#define TXB0EID8	0x33

#define TXB0EID0	0x34

#define TXB0DLC		0x35

#define TXB0D0		0x36

#define TXB0D1		0x37

#define TXB0D2		0x38

#define TXB0D3		0x39

#define TXB0D4		0x3A

#define TXB0D5		0x3B

#define TXB0D6		0x3C

#define TXB0D7		0x3D

#define	RTR			6
#define MCP_BITMOD		0x05


#define MSG_ID 0
#define MSG_LEN 4
#define MSG_DATA 5

//#define	bit_is_set(pin, bit)	(pin & (1<<bit))

#define MCP2515_READ_STATUS 0xA0

#define MCP_RX0IF		0x01 

#define MCP_STAT_RX0IF (1<<0)
/** \brief	Bitdefinition von CANINTF */
#define MERRF		7
#define WAKIF		6
#define ERRIF		5
#define TX2IF		4
#define TX1IF		3
#define TX0IF		2
#define RX1IF		1
#define RX0IF		0

// Begin mt
#define MCP_SIDH        0
#define MCP_SIDL        1
#define MCP_EID8        2
#define MCP_EID0        3

#define MCP_DLC_MASK        0x0F

uint8_t spic_master_tx_rx(uint8_t c);
void mcp_reset(const uint8_t chip);
uint8_t mcp2515_readRegister(const uint8_t chip,const uint8_t address);
void mcp2515_setRegister(const uint8_t chip,const uint8_t address, const uint8_t value);

void mcp2515_setRegisterS(const uint8_t chip,const uint8_t address,
const uint8_t values[], const uint8_t n);
void mcp2515_modifyRegister(const uint8_t chip,const uint8_t address,
const uint8_t mask, const uint8_t data);
void mcp2515_init(const uint8_t chip);
uint8_t mcp2515_read_rx_status(const uint8_t chip);
void can_send_message(const uint8_t chip,uint8_t *p_message);
uint8_t read_status(const uint8_t chip,const uint8_t cmd);
void mcp2515_readRegisterS(const uint8_t chip,const uint8_t address,
uint8_t values[], const uint8_t n);
uint8_t can_get_message(const uint8_t chip,CAN_MSG *p_message);
void mcp2515_clear_interrupt(uint8_t chip);


#endif /* CAN_H_ */